package com.xiaotu.spider.maoyan.pipeline;

import java.util.List;
import java.util.stream.Collectors;

import com.xiaotu.common.model.*;
import com.xiaotu.common.util.SepratorUtil;
import org.springframework.stereotype.Component;

import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.spider.APipeline;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

/**
 * @类名 BoxOfficePipeline
 * @日期 2017年7月31日
 * @作者 高海军
 * @功能 票房数据pipeline
 */
@Component("BoxOfficePipeline")
public class BoxOfficePipeline extends APipeline
{
    @Override
    protected void doProcess(ResultItems resultItems, Task task)
    {
        /*List<MovieRecordModel> list = resultItems.get(MovieRecordModel.class.getName());
        if (list != null && !list.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_BOXOFFICE_YYYYMMDD);
            cacheHandler.hMSet(key, list.stream().collect(Collectors.toMap
                    (MovieRecordModel::getTvName, mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }*/

        List<MovieCityModel> cityList = resultItems.get(MovieCityModel.class.getName());
        if (cityList != null && !cityList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_AREA_YYYYMMDD);
            cacheHandler.hMSet(key, cityList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getAreaName(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }

        List<MovieCityModel> companyList = resultItems.get(MovieCityModel.class.getName() +
                Constants.PageDataType.MOVIE_COMPANY);
        if (companyList != null && !companyList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_COMPANY_YYYYMMDD);
            cacheHandler.hMSet(key, companyList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getAreaName(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }

        MoviePeopleModel people = resultItems.get(MoviePeopleModel.class.getName());
        if (people != null)
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_PEOPLE_YYYYMMDD);
            cacheHandler.hSet(key, people.getDataId(), people);
            cacheHandler.setCacheTimeout(key);
        }

        MovieSplitModel split = resultItems.get(MovieSplitModel.class.getName());
        if (split != null)
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_SPLIT_YYYYMMDD);
            cacheHandler.hSet(key, split.getDataId(), split);
            cacheHandler.setCacheTimeout(key);
        }

        List<MovieMarketModel> marketList = resultItems.get(MovieMarketModel.class.getName());
        if (marketList != null && !marketList.isEmpty())
        {
            String key = DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_MARKET_YYYYMMDD);
            cacheHandler.hMSet(key, marketList.stream().collect(Collectors.toMap(mov -> mov
                    .getDataId() + SepratorUtil.SEP_UNDERLINE + mov.getDate() + SepratorUtil
                    .SEP_UNDERLINE + mov.getTitle(), mov -> mov)));
            cacheHandler.setCacheTimeout(key);
        }

    }

}